package com.yjps.system.controller;

import com.alibaba.fastjson.JSON;
import com.yjps.common.message.Message;
import com.yjps.common.page.Pager;
import com.yjps.system.model.Code;
import com.yjps.system.model.CodeType;
import com.yjps.system.service.CodeService;
import com.yjps.system.service.CodeTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Random;

/**
 * 码表控制类
 * Created by jessy on 2015/8/22.
 */
@Controller
@RequestMapping("/system/code")
public class CodeController {

    Logger log = LoggerFactory.getLogger(CodeController.class);
    @Autowired
    CodeService codeService;
    @Autowired
    CodeTypeService codeTypeService;

    /**
     * 分页显示码表数据
     * @param model
     * @param request  用于传递分页数据参数
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception {
        Pager pager = codeService.getUserByPager(new Pager<Code>(request,response));
        List<Code> codeList = /*codeService.codeList();*/pager.getList();
        for(Code code:codeList){
            CodeType ct = codeTypeService.findCodeType(code.getCodetypeId());
            code.setViewCodeType(ct.getCodetypeName());
        }
//        model.addAttribute("codeList",codeList);
        model.addAttribute(pager);
        return "system/basecode/codeList";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String editForm(Model model){
            List<CodeType> codetypes = codeTypeService.codetypeList();
            if(codetypes != null){
                model.addAttribute("codetypes", codetypes);
            }
        return "system/basecode/edit";
    }

    /**
     * 添加码表
     * @param code
     * @param codetypeId  选择已存在的码表类
     * @param codetypeId2 自定义码表类
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Code code,String codetypeId,String codetypeId2,HttpServletRequest request,Model model){
        if(!StringUtils.isEmpty(codetypeId)){
            if(codetypeId.equals("00")) {
                if (codetypeId2.equals("")) {

                    return "redirect:/system/code/edit";
                }else{
                    CodeType c = new CodeType();
                    c.setCodetypeName(codetypeId2);
                    c.setCodetypeState(1);
                    c.setIsValid(1);
                    c.setCodetypeDescription("");
                    int id = codeTypeService.addCodeType(c);
                    code.setCodetypeId(id);
                }
            }else{
                code.setCodetypeId(Integer.parseInt(codetypeId));
            }
                code.setIsValid(1);
                code.setCodeState(1);
                codeService.addCode(code);
                log.debug("add code success  id is " + code.getId());
        }else{
            log.debug("add code fail ");
        }
        return "redirect:/system/code/list";
    }

    /**
     * 更新码表
     * @param code
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Code code){

        codeService.updateCode(code);
        return "redirect:/system/code/list";
    }

    /**
     * 删除指定id的码表记录
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(@RequestParam(value = "id",required = true)String id,Model model){
        Message message = null;
        codeService.deleteCode(Integer.parseInt(id));
        message = new Message(true,"delete successfully ");
        model.addAttribute("message",message);
        return "redirect:/system/code/list";
    }

    /**
     * 获得指定id的码表json对象
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCode",method = RequestMethod.GET)
    public String getCode(@RequestParam(value = "id",required = true)String id){
        Code c = codeService.findCode(Integer.parseInt(id));
        CodeType ct = codeTypeService.findCodeType(c.getCodetypeId());
        c.setViewCodeType(ct.getCodetypeName());
        String jsonString  = JSON.toJSONString(c);
        System.out.println("code json string is " +jsonString);
        return jsonString;
    }
}
